package com.mypage.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.main.MainUIActivity;
import com.cloudcc.mobile.view.schedule.NewEventActivity;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.litesuits.android.log.Log;
import com.mypage.bean.SafteCodeBean;
import com.mypage.bean.SuccessBean;
import com.mypage.utils.UtilsShowDialog;

public class ValidationActivity extends Activity {
	public Button mBtnBack, safteySubmat;
	public EditText etScode;
	public TextView bar_title_text;
	private UtilsShowDialog showDialog;
	private String mEns = RunTimeManager.getInstance().getlanguage();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.validation_activity);
		showDialog = new UtilsShowDialog(this);
		initView();
		initListener();
	}

	private void initView() {
		// TODO Auto-generated method stub
		mBtnBack = (Button) findViewById(R.id.mBtnBack);
		etScode = (EditText) findViewById(R.id.etScode);
		safteySubmat = (Button) findViewById(R.id.safteySubmat);
		bar_title_text = (TextView) findViewById(R.id.bar_title_text);
		if ("en".equals(mEns)) {
			bar_title_text.setText("Bind device binding device");
		}else{
			bar_title_text.setText("绑定设备");
		}
		

	}

	private void initListener() {
		// TODO Auto-generated method stub
		mBtnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		safteySubmat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String mCode = etScode.getText().toString().trim();
				sendHttp(mCode);
			}
		});
	}

	public void sendHttp(String mCode) {
	
		if ("".equals(mCode)) {
			if ("en".equals(mEns)) {
				Toast.makeText(this, "The security code is empty", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(this, "安全码为空", Toast.LENGTH_SHORT).show();
			}
			
		} else {
			if ("en".equals(mEns)) {
				showDialog.onShowLoadingDialog("loading...");
			}else{
				showDialog.onShowLoadingDialog("正在加载...");
			}
			
			HttpUtils utils = new HttpUtils();
			RequestParams params = new RequestParams();
			params.addBodyParameter("binding", RunTimeManager.getInstance().getServerBinding());
			params.addBodyParameter("serviceName", "mfaVerification");
			params.addBodyParameter("verificationCode", mCode);
			utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
					new RequestCallBack<String>() {

						@Override
						public void onFailure(HttpException arg0, String arg1) {
							// TODO Auto-generated method stub
							showDialog.onDismissLoadingDialog();
							Log.d("发送安全码接口失败-------------------------", arg1);
						}

						@Override
						public void onSuccess(ResponseInfo<String> arg0) {
							showDialog.onDismissLoadingDialog();
							Log.d("发送安全码接口成功-------------------------", arg0.result);
							SafteCodeBean mData = new Gson().fromJson(arg0.result, SafteCodeBean.class);
							mJudge(mData);
						}
					});
		

		}
	}
	public void mJudge(SafteCodeBean mData){
		if (mData.result) {
			Intent intent = new Intent(this, MainUIActivity.class);
			intent.putExtra("activity.from", 2);
			this.startActivity(intent);
			Toast.makeText(this, mData.returnInfo, Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(this, mData.returnInfo, Toast.LENGTH_SHORT).show();
		}
	}
}
